
import React from 'react';
import { Button } from 'antd';
import {
  DownloadOutlined,
} from '@ant-design/icons';
import { LogAddCardSearch } from '../BackAPI';
export const isImg = /([\w-]+\.)+[\w-]+(jpg|png|jpeg|svg)?/;
export const getChildrenToRender = (item, i) => {
  let tag = item.name.indexOf('title') === 0 ? 'h1' : 'div';
  tag = item.href ? 'a' : tag;
  let children = typeof item.children === 'string' && (['image', 'img'].includes(item?.name) || item.children.match(isImg))
    ? React.createElement('img', { src: item.children, alt: 'img' })
    : item.children;
  if (item.name.indexOf('button') === 0 && typeof item.children === 'object') {
    children = React.createElement(Button, {
      ...item.children
    });
  }
  return React.createElement(tag, { key: i.toString(), ...item }, children);
};



export const LogButton = (props) => {
  const objChild = { ...(props ?? {}) };
  let { text, softName, href, target, icon = <DownloadOutlined /> } = objChild;
  if (!!softName) {
    href = href ?? `/soft/${softName}`;
    target = target ?? "_blank";
  }


  ["isLogButton", "text", 'softName'].forEach(key => delete objChild[key]);

  return React.createElement(Button, {
    href,
    target,
    icon,
    ...objChild,
    onClick: (e) => {

      LogAddCardSearch("", "下载文件", {
        softName

      })

      if (objChild.onClick) {
        objChild.onClick(e);
      }
    }
  }, !!text ? text : props.children);
}

